package com.clusterswisdom.horn.api.tmall.action.logistics;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.clusterswisdom.horn.api.ioc.HornAction;
import com.clusterswisdom.horn.api.ioc.HornResponse;
import com.clusterswisdom.horn.api.ioc.api.ApiConfig;
import com.clusterswisdom.horn.api.tmall.TmallInstance;
import com.clusterswisdom.horn.api.tmall.action.AbstractTmallAction;
import com.clusterswisdom.horn.api.tmall.action.logistics.event.Company;
import com.taobao.api.domain.LogisticsCompany;
import com.taobao.api.response.LogisticsCompaniesGetResponse;

public class GetlogisticsCompaniesAction extends AbstractTmallAction implements HornAction{
	@Override
	public HornResponse execute(ApiConfig config, JSONObject param)
			throws Exception {
		LogisticsCompaniesGetResponse response = (LogisticsCompaniesGetResponse)TmallInstance.getInstance().executeRequest(param, "entityProduct", "logisticsCompaniesGet");
		
		if(response.isSuccess()){
			JSONObject responseDate = new JSONObject();
			JSONArray copCodeList = new JSONArray();
	 		for(LogisticsCompany company : response.getLogisticsCompanies()){
	 			copCodeList.add(new Company(company.getCode(),company.getName()));
			}
	 		responseDate.put("copCodeList", copCodeList);
			return new HornResponse(true, responseDate);
		}else{
			
			return getUnsuccessResponse(response);
		}
	}
}
